﻿#define DEV
using SimpleTerminal.Library;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Win32;
using static SimpleTerminal.Controls.TabLayout;

namespace SimpleTerminal.Controls
{
    public partial class TabLayoutGroup : Control
    {

        bool sigleViewMode = true;

        TabLayout[] tabLayouts = new TabLayout[2];

        public event EventHandler<TabEventArgs> SelectChanged;
        public event EventHandler<TabEventArgs> TabClosing;
        public event EventHandler<TabEventArgs> TabCreated;

        private Color tabsBarBackgroundColor = SystemColors.Control;

        [Category("自定义属性")]
        [Description("标签栏的背景色")]
        public Color TabBarBackgroundColor {
            get { return tabsBarBackgroundColor; }
            set { 
                tabsBarBackgroundColor = value;
                tabLayouts[0].TabBarBackgroundColor = value;
                if (!sigleViewMode)
                {
                    tabLayouts[1].TabBarBackgroundColor = value;
                }
            }
        }

        private TabLayout activedTabLayout = null;
        private int activedIndex = 0;

        protected int ActivedIndex { get => activedIndex; set => activedIndex = value; }
        public TabLayout ActivedTabLayout { get => activedTabLayout; set => activedTabLayout = value; }
#if DEV
        public int Panel1MinSize { get => panel1MinSize; set => panel1MinSize = value; }
        public int Panel2MinSize { get => panel2MinSize; set => panel2MinSize = value; }
        public bool Panel1Collapsed
        {
            get => panel1Collapsed;
            set
            {
                SuspendLayout();
                if (value)
                {
                    panel1.Visible = false;
                    splitter.Visible = false;
                    panel2.Width = Width;
                    panel2.Height = Height;
                    panel2.Location = new Point(0, 0);
                }
                else
                {
                    //panel1.Dock = DockStyle.Fill;

                    panel1.Width = (Width - splitter.Width) / 2;
                    panel1.Height = Height;
                    panel1.Visible = true;


                    splitter.Location = new Point(panel1.Width, 0);

                    panel2.Location = new Point(panel1.Width + splitter.Width, 0);
                    panel2.Width = Width - panel2.Location.X;
                    panel2.Height = Height;
                    splitter.Height = Height;

                    if (!panel2Collapsed)
                        splitter.Visible = true;
                }
                panel1Collapsed = value;
                ResumeLayout(false);
            }
        }
        public bool Panel2Collapsed
        {
            get => panel2Collapsed;
            set
            {
                SuspendLayout();
                if (value)
                {

                    panel2.Visible = false;
                    splitter.Visible = false;
                    panel1.Dock = DockStyle.Fill;
                    panel1.Width = Width;
                    panel1.Height = Height;
                }
                else
                {

                    panel1.Dock = DockStyle.Left;
                    panel1.Width = (Width - splitter.Width) / 2;
                    panel1.Height = Height;
                    /// 
                    if (!panel1Collapsed)
                        splitter.Visible = true;
                    splitter.Location = new Point(panel1.Width, 0);

                    panel2.Visible = true;
                    panel2.Location = new Point(splitter.Width + panel1.Width, 0);
                    panel2.Width = Width - panel2.Location.X;
                    panel2.Height = Height;
                    panel2.BringToFront();
                    splitter.Height = Height;
                }
                panel2Collapsed = value;
                ResumeLayout(false);
            }
        }
#endif
        private void TabLayout_SelectChanged(object sender, TabEventArgs e)
        {
            SelectChanged?.Invoke(sender, e);
        }
        private void TabLayout_Actived(object sender, EventArgs e)
        {
            ActiveTabLayout((TabLayout)sender);
        }

        public void AddItem(PageContainer item)
        {
            AddTabPage(item);
        }
        public void AddTabPage(PageContainer item)
        {
            activedTabLayout.AddItem(item);
        }



        public void ActiveTabLayout(TabLayout tabLayout)
        {
            ActiveTabLayout(Array.IndexOf(tabLayouts, tabLayout));
        }
        public void ActiveTabLayout(int index)
        {
            ActivedIndex = index;
            if (activedTabLayout != null && activedTabLayout != tabLayouts[index])
                activedTabLayout.Active = false;
            activedTabLayout = tabLayouts[index];
            //Console.WriteLine("激活了控件");
        }
#if DEV
        class SplitterPanel : Panel {
            public new CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    cp.Style |= User32.WS_CLIPCHILDREN;
                    return cp;
                }
            }
        }
        Panel panel1 = new SplitterPanel();
        Panel panel2 = new SplitterPanel();
        Splitter splitter = new Splitter();
        public void Initialize()
        {
            SuspendLayout();

            panel1.Size = new Size((Width - 5) / 2, Height);
            panel1.TabIndex = 1;
            panel1.Dock = DockStyle.Left;
            // panel1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
            //panel1.BackColor = Color.Black;
            panel1.Location = new Point(0, 0);
            panel1.SizeChanged += Panel1_SizeChanged;
            //panel1.Size = new Size();

            splitter.BackColor = Color.DimGray;
            splitter.Height = Height;
            splitter.Width = 3;
            splitter.Location = new Point(panel1.Width, 0);
            splitter.Dock = DockStyle.Left;
            splitter.TabIndex = 2;
            splitter.SplitterMoved += Splitter_Moved;
            pos = 0.5;

            panel2.Size = new Size((Width - 5) / 2, Height);
            panel2.Height = Height;
            panel2.Dock = DockStyle.Fill;
            panel2.TabIndex = 3;
            panel2.Location = new Point(panel1.Width + splitter.Width, 0);
            // panel2.BackColor = Color.Navy;
            //panel2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;

            splitter.MinSize = 400;
            splitter.MinExtra = 400;

            Controls.Add(splitter);
            Controls.Add(panel1);
            // panel1.Dock = DockStyle.Fill;
            Controls.Add(panel2);
            ResumeLayout(false);
        }
        private double pos = 0;
        private int panel1MinSize = 400;
        private int panel2MinSize = 400;

        private void Splitter_Moved(object sender, SplitterEventArgs e)
        {
            pos = (double)e.X / Width;
        }
#endif
        private void Panel1_SizeChanged(object sender, EventArgs e)
        {

            // Console.WriteLine("比率"+pos);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
#if DEV
            splitter.SplitPosition = (int)(Width * pos);
#endif
        }

        public TabLayoutGroup()
        {

            InitializeComponent();
#if DEV
            Initialize();
            InitViewMode();
#else
            SuspendLayout();
            splitContainer = new SplitContainer();
            splitContainer.Width = Width;
            splitContainer.Height = Height;
            splitContainer.Orientation = Orientation.Vertical; ;
            splitContainer.Dock = DockStyle.Fill;
            splitContainer.SplitterDistance = Width / 2;
            splitContainer.SplitterWidth = 2;
            splitContainer.Panel1MinSize = 400;
            splitContainer.Panel2MinSize = 400;
            splitContainer.Panel1.BackColor = Color.White;
            splitContainer.Panel2.BackColor = Color.White;
            splitContainer.BackColor = SystemColors.ActiveBorder;
            splitContainer.TabStop = false;
            Controls.Add(splitContainer);
            ResumeLayout(false);
            InitViewMode();
#endif
        }
        private TabLayout CreateTabLayout()
        {
            TabLayout tabLayout = new TabLayout
            {
                TabStop = true,
                Location = new Point(0, 0),
                Dock = DockStyle.Fill,
                // tabLayout.Size = new Size(Width / 2 - 1, Height);
                Margin = new Padding(0)
            };
            tabLayout.SelectChanged += TabLayout_SelectChanged;
            tabLayout.TabToOther += TabLayout_ToOther;
            tabLayout.TabCreated += TabLayout_Created;
            tabLayout.TabClosing += TabLayout_Closing;
            tabLayout.TabClosed += TabLayout_Closed;
            tabLayout.Actived += TabLayout_Actived;
            tabLayout.TabBarBackgroundColor = tabsBarBackgroundColor;
            return tabLayout;
        }
        protected override void InitLayout()
        {
            base.InitLayout();
        }

        private void TabLayout_Created(object sender, TabEventArgs e)
        {
            TabCreated?.Invoke(sender, e);
        }
        private void TabLayout_Closed(object sender, TabEventArgs e)
        {
            TabLayout tabLayout = sender as TabLayout;
            if (!sigleViewMode && tabLayout.Items.Count == 0)
            {
                SigleViewMode(Array.IndexOf(tabLayouts, sender));
            }
        }
        private void TabLayout_Closing(object sender, TabEventArgs e)
        {
            TabLayout tabLayout = sender as TabLayout;
            TabClosing?.Invoke(sender, e);
        }

        private bool panel1Collapsed = false;
        private bool panel2Collapsed = false;


        private void InitViewMode()
        {
            SuspendLayout();
#if DEV
            tabLayouts[0] = CreateTabLayout();
            Panel2Collapsed = true;
            panel1.Controls.Add(tabLayouts[0]);
#else
            tabLayouts[0] = CreateTabLayout();
            splitContainer.Panel2Collapsed = true;
            splitContainer.Panel1.Controls.Add(tabLayouts[0]);
#endif
            ActiveTabLayout(0);
            ResumeLayout(false);
        }
        private void SigleViewMode(int index)
        {
            int deleteIndex = index;
            int retainIndex = index == 0 ? 1 : 0;
            SuspendLayout();
            // panel1.SuspendLayout();
            //  panel2.SuspendLayout();
#if DEV
            sigleViewMode = true;
            tabLayouts[retainIndex].AddItems(tabLayouts[deleteIndex].Items);
            if (retainIndex == 0)
            {
                tabLayouts[1].Dispose();
                //Panel2Collapsed = true;
                //panel2.Controls.Clear();
                //tabLayouts[1].Dispose();
                //ActiveTabLayout(0);
            }
            else
            {
                panel1.Controls.Clear();
                tabLayouts[0].Dispose();
                tabLayouts[0] = tabLayouts[1];
                panel1.Controls.Add(tabLayouts[0]);
                //Panel1Collapsed = true;
                //panel1.Controls.Clear();
                // tabLayouts[0].Dispose();
                // ActiveTabLayout(1);
            }
            panel2.Controls.Clear();
            Panel2Collapsed = true;
            ActiveTabLayout(0);
#else
            sigleViewMode = true;
            tabLayouts[retainIndex].AddItems(tabLayouts[deleteIndex].Items);

            if (retainIndex != 0)
            {
                tabLayouts[0].Dispose();
                tabLayouts[0] = tabLayouts[1];
                splitContainer.Panel1.Controls.Clear();
                splitContainer.Panel1.Controls.Add(tabLayouts[0]);
            }
            else
            {
                tabLayouts[1].Dispose();
            }
            splitContainer.Panel2.Controls.Clear();
            splitContainer.Panel2Collapsed = true;
            ActiveTabLayout(0);

#endif
            //panel1.ResumeLayout(false);
            // panel2.ResumeLayout(false);
            ResumeLayout(false);
        }
        private int Other(int i)
        {
            return i == 1 ? 0 : 1;
        }
        private void DobuleViewMode(PageContainer item)
        {
            SuspendLayout();
            sigleViewMode = false;

            int curr = Array.IndexOf(tabLayouts, activedTabLayout);
            //int curr = 1;
            tabLayouts[curr].Remove(item);
            if (curr == 0)
            {
                tabLayouts[0].Remove(item);
                tabLayouts[1] = CreateTabLayout();
                panel2.Controls.Add(tabLayouts[1]);
                Panel2Collapsed = false;
                ActiveTabLayout(1);
                //Panel1Collapsed = true;
            }
            else
            {
                tabLayouts[1].Remove(item);
                tabLayouts[0] = CreateTabLayout();
                tabLayouts[0].AddItem(item);
                panel1.Controls.Add(tabLayouts[0]);
                // Panel1Collapsed = true;
                Panel1Collapsed = false;
                ActiveTabLayout(0);
            }



            ResumeLayout(false);

            //放在这里 用于获取焦点
            tabLayouts[1].AddItem(item);

        }

        private void TabLayout_ToOther(object sender, TabEventArgs e)
        {
            TabLayout tabLayout = (TabLayout)sender;

            if (sigleViewMode)
            {
                if (tabLayout.Items.Count > 1)
                {
                    DobuleViewMode(e.Item);
                }
            }
            else
            {
                if (tabLayout.Items.Count == 1)
                {
                    SigleViewMode(Array.IndexOf(tabLayouts, tabLayout));
                }
                else
                {
                    tabLayouts[0].SuspendLayout();
                    tabLayouts[1].SuspendLayout();
                    tabLayout.Remove(e.Item);
                    if (tabLayouts[0] == tabLayout)
                    {
                        tabLayouts[1].AddItem(e.Item);
                    }
                    else
                    {
                        tabLayouts[0].AddItem(e.Item);
                    }
                    tabLayouts[0].ResumeLayout(false);
                    tabLayouts[1].ResumeLayout(false);
                }

            }
        }
        private void Control_GotFocus(object sender, EventArgs e)
        {
            if (!sigleViewMode)
            {

                // Terminal terminal = (Terminal)sender;
                // ActiveTabLayout((int)terminal.Tag);
            }
            else
            {
                ActiveTabLayout(0);
            }

        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            Console.WriteLine("TAB  GotFocus");
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }




        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            Console.WriteLine("OnMouseDown");
        }

        internal void SetLableActive(PageContainer item, bool ac)
        {
            if (item == null && item.TabLayout == null)
                return;
            item.TabLayout.GetTabLabelByItem(item).IndicatorActive = ac;
        }

        public PageContainer Find(string name)
        {
            foreach (TabLayout tabLayout in tabLayouts)
            {
                PageContainer pageContainer = tabLayout.Find(name);
                if (pageContainer == null)
                    continue;
                return pageContainer;
            }
            return null;
        }
    }
}
